package com.fairytask.engine.components.radioButton
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class RadioButton extends BaseComponent
	{
		private var _bgSpace:Sprite=new Sprite();
		private var rec:Sprite; 	// 实心圆 
		private var gir:Sprite; 	//空心圆
		private var _skinMc:Sprite;	//皮肤
		private var _enabled:Boolean=true;//默认可点
		
		private var _group:RadioButtonGroup; //radioButtond的组
		
		//显示
		private var _labelMc			:Label;
		private var _unSelectSkin	:MovieClip;
		private var _overSkin		:MovieClip;
		private var _selectSkin		:MovieClip;
		private var _currentSkin	:MovieClip;
		private var _hotArea		:Sprite	=	new Sprite();
		private var _select			:Boolean;
		private var _isEvent		:Boolean=	true;
		
		public function RadioButton(str:String,pSkin:String=null,strHover:Boolean=false,overEffect:Boolean=false)
		{
			
			super();
			_labelMc	=	new Label();
			_labelMc.htmlStrByBorder	=	str;
			mouseChildren	=	true;
			_bgSpace.buttonMode=false;
			
			strSkin(pSkin,strHover,overEffect);
			initEvent(overEffect);
			buttonMode	=	true;
		}
		
		public function onSelect(evt:MouseEvent):void
		{
			select	=	_select?false:true;
		}
		/**
		 * 点击事件 
		 * @param e
		 * 
		 */
		public function onClick(e:Event):void
		{
			if(_group!=null)
			{
				if(_enabled==true)
				{
					_group.dispatchEvent(new RadioButtonEvent(RadioButtonEvent.RADIO_CLICK,this));
				}
			}
		}
		
		/**是否禁用**/
		override public function set enabled(val:Boolean):void
		{
			_enabled = val;
			if(_enabled)
			{
				filters	=	null;
			}
			else
			{
				filters	=	[Filter.GRAY];
			}
		}
		
		override public function get enabled():Boolean
		{
			return _enabled;
		}
		
		/**
		 *绘制按钮 
		 * @param x
		 * @param y
		 * @param nullRadii
		 * @param fullRadii
		 * @param labStr
		 * @param labX
		 * @param labY
		 * @param group
		 * 
		 */		
		public function createRadio(c:uint):void
		{			       		
			drawFillCircle(c);	
		}
		/**
		 *  绘制空心圆 
		 * @param x
		 * @param y
		 * @param radii
		 * @param color
		 * 
		 */		
		public function drawCircle():void
		{				
			gir=new Sprite();
			gir.graphics.lineStyle(2,0xFFee66);
			gir.graphics.drawCircle(0,0,6);
			_bgSpace.addChild(gir);
		}
		
		
		public function strSkin(skin:String,isHover:Boolean,overEffect:Boolean):void
		{
			if(skin!=null)
			{
				var mc:MovieClip= ResManager.newResClass(skin) as MovieClip;
				_unSelectSkin	=	mc.getChildByName("CheckBox_unselected") as MovieClip;
				_selectSkin		=	mc.getChildByName("CheckBox_selected") as MovieClip;
				if(overEffect)
				{
					_overSkin	=	mc.getChildByName("CheckBox_over") as MovieClip;
					_overSkin.y	=	_overSkin.x	=	0;
				}
				
				_unSelectSkin.x		=	_selectSkin.x =0;
				_unSelectSkin.y		=	_selectSkin.y =0;
				
				_hotArea	=	draw(_unSelectSkin.width,_unSelectSkin.height);
				
//				addChild(_hotArea);
				addChild(_bgSpace);
				var delta		:int	=	int((_labelMc.height-_hotArea.height)/2);
				_labelMc.y	=	delta>0?(-delta):delta;
				addChild(_labelMc);
				_bgSpace.addChild(_unSelectSkin);
				_currentSkin=_unSelectSkin;
				
				//文字标签位置
				if(isHover)
				{
					_labelMc.x	=	(_unSelectSkin.width-	_labelMc.width)*0.5;
				}
				else
				{
					_labelMc.x	=	_bgSpace.width;
				}
			}
			else
			{
				drawCircle();			
				drawFillCircle(0);	
			}
		}
		
		public function initEvent(overEffect:Boolean):void
		{
			_isEvent	=	true;
			addEventListener(MouseEvent.CLICK,onClick);
			if(overEffect)
			{
				_bgSpace.addEventListener(MouseEvent.ROLL_OVER,onOver);
				_bgSpace.addEventListener(MouseEvent.ROLL_OUT,onOut);
			}
			
		}
		
		private function draw(w:int,h:int):Sprite
		{
			var bin:Sprite	=	new Sprite();
			bin.graphics.beginFill(0,0.5);
			bin.graphics.drawRect(0,0,w,h);
			bin.graphics.endFill();
			return bin;
		}
		/**
		 * 绘制实心圆 
		 * @param x
		 * @param y
		 * @param radii
		 * 
		 */		
		public function drawFillCircle(c:uint):void
		{
			rec=new Sprite();
			rec.graphics.beginFill(c);
			rec.graphics.drawCircle(0,0,5);
			rec.graphics.endFill();
			_bgSpace.addChild(rec);
		}
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}
		public function get group():RadioButtonGroup
		{
			return _group;
		}
		
		public function set group(group:RadioButtonGroup):void
		{
			if(group==null)
			{
				return;
			}
			_group = group;
			_group.addRadioButton(this);
			
		}

		public function get select():Boolean
		{
			return _select;
		}

		public function set select(value:Boolean):void
		{
			_select = value;
			
			clearBG();
			if(_select)
			{
				_bgSpace.addChild(_selectSkin);
				_currentSkin	=	_selectSkin;	
			}
			else
			{
				_bgSpace.addChild(_unSelectSkin);
				_currentSkin	=	_unSelectSkin;
			}
		}
		
		private function onOver(evt:MouseEvent):void
		{
			if(_currentSkin	==	_selectSkin)
			{
				return;				
			}
			clearBG();
			
			_bgSpace.addChild(_overSkin);
					
		}
		
		private function onOut(evt:MouseEvent):void
		{
			clearBG();
			_bgSpace.addChild(_currentSkin);
		}
		
		override public function set label(	value:String):void
		{
			_labelMc.htmlStr	=	value;			
		}
		
		public function set labelTop(val:int):void
		{
			_labelMc.y	=	val;
		}

	}
}